<?php

/**
 * Cita form base class.
 *
 * @method Cita getObject() Returns the current form's model object
 *
 * @package    toyomotor
 * @subpackage form
 * @author     Ing. Roberto Hernández De La Luz at Netosfera
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseCitaForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id_cita'             => new sfWidgetFormInputHidden(),
      'fecha'               => new sfWidgetFormDate(),
      'hora'                => new sfWidgetFormInputText(),
      'fecha_entrega'       => new sfWidgetFormDate(),
      'hora_entrega'        => new sfWidgetFormInputText(),
      'hora_preferida'      => new sfWidgetFormInputText(),
      'detalles'            => new sfWidgetFormTextarea(),
      'observacion'         => new sfWidgetFormTextarea(),
      'detalles_usuario'    => new sfWidgetFormInputText(),
      'kilometraje'         => new sfWidgetFormInputText(),
      'status'              => new sfWidgetFormInputText(),
      'is_aceptado'         => new sfWidgetFormInputCheckbox(),
      'is_pending'          => new sfWidgetFormInputCheckbox(),
      'encuesta'            => new sfWidgetFormInputCheckbox(),
      'claveseguridad'      => new sfWidgetFormInputText(),
      'created_at'          => new sfWidgetFormDateTime(),
      'id_usuario'          => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Usuario'), 'add_empty' => false)),
      'id_vehiculo_usuario' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('VehiculoUsuario'), 'add_empty' => false)),
      'id_vendedor'         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Vendedor'), 'add_empty' => true)),
      'servicios_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Servicio')),
    ));

    $this->setValidators(array(
      'id_cita'             => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id_cita')), 'empty_value' => $this->getObject()->get('id_cita'), 'required' => false)),
      'fecha'               => new sfValidatorDate(),
      'hora'                => new sfValidatorString(array('max_length' => 8)),
      'fecha_entrega'       => new sfValidatorDate(array('required' => false)),
      'hora_entrega'        => new sfValidatorString(array('max_length' => 8, 'required' => false)),
      'hora_preferida'      => new sfValidatorString(array('max_length' => 20, 'required' => false)),
      'detalles'            => new sfValidatorString(array('max_length' => 500, 'required' => false)),
      'observacion'         => new sfValidatorString(array('max_length' => 500, 'required' => false)),
      'detalles_usuario'    => new sfValidatorString(array('max_length' => 200, 'required' => false)),
      'kilometraje'         => new sfValidatorInteger(),
      'status'              => new sfValidatorInteger(array('required' => false)),
      'is_aceptado'         => new sfValidatorBoolean(array('required' => false)),
      'is_pending'          => new sfValidatorBoolean(array('required' => false)),
      'encuesta'            => new sfValidatorBoolean(array('required' => false)),
      'claveseguridad'      => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'created_at'          => new sfValidatorDateTime(),
      'id_usuario'          => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Usuario'))),
      'id_vehiculo_usuario' => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('VehiculoUsuario'))),
      'id_vendedor'         => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Vendedor'), 'required' => false)),
      'servicios_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Servicio', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('cita[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Cita';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['servicios_list']))
    {
      $this->setDefault('servicios_list', $this->object->Servicios->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveServiciosList($con);

    parent::doSave($con);
  }

  public function saveServiciosList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['servicios_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Servicios->getPrimaryKeys();
    $values = $this->getValue('servicios_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Servicios', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Servicios', array_values($link));
    }
  }

}
